package com.deman.framework.web;


import com.deman.framework.api.ICaculatorService;
import com.deman.framework.api.IHelloService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.CompletableFuture;

@RestController
@SuppressWarnings("all")
public class HelloController  {
    Logger logger= LoggerFactory.getLogger(HelloController.class);
   @Autowired
    private IHelloService helloService;

    @Autowired
    private ICaculatorService caculatorService;

    @GetMapping(value="/hello" )
    public  String  hello(){
      String rt=null;
      try{
         rt= helloService.hello("hello");
      }catch (Exception e){
          logger.error(e.getMessage());
      }
        return rt;
    }

    @GetMapping(value="/asynHello" )
    public DeferredResult<String> asynHello(){
        DeferredResult<String> result=new DeferredResult <>();
        CompletableFuture.supplyAsync(()->{
           String reslut=null;
           try {
               reslut=  helloService.hello("hello");
           }catch (Exception e){
               logger.error(e.getMessage());
           }
            return  reslut;
        }).thenAccept(e->{
            result.setResult(e);
        });
        return  result ;
    }

    @GetMapping(value="/add/{x}/{y}" )
    public  String  add(@PathVariable("x") int x,@PathVariable("y") int y){
        return caculatorService.add(x,y)+"";
    }
}
